<?php

declare(strict_types=1);

namespace app\controller\api;

use QL\QueryList;
use think\Exception;
use think\facade\App;
use think\facade\Db;
use think\Response;

class Test extends Base
{

    public function index(): Response
    {
        $data = Db::name('fx_users')->select()->toArray();

        $version = config('apiadmin.APP_VERSION');
        if (!$version) {
            throw new Exception('请先执行安装脚本，完成项目初始化！');
        } else {
            return $this->buildSuccess([
                'Product'    => config('apiadmin.APP_NAME'),
                'ApiVersion' => $version,
                'TpVersion'  => App::version(),
                'Company'    => config('apiadmin.COMPANY_NAME'),
                'ToYou'      => "（终于等到你2244！22）"
            ]);
        }
    }

    public function test2(): Response
    {
        https://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=11&dwmc=%E5%8C%97%E4%BA%AC%E7%A7%91%E6%8A%80%E5%A4%A7%E5%AD%A6&mldm=01&mlmc=&yjxkdm=0101&xxfs=&zymc=
        $param = $this->request->param();
        // $ssdm = $param['ssdm']; //省分 例如北京11
        // $mldm = $param['mldm']; //03  //门类
        // $yjxkdm = $param['yjxkdm'];  //学科类别
        // $pageno = $param['pageno'];//页数
        // $dwmc = $param['dwmc'];  //大学名称
        // $xxfs = $param['xxfs'];   
        // $url = $param['url']; //https://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=11&dwmc=%E5%8C%97%E4%BA%AC%E5%A4%A7%E5%AD%A6&mldm=01&mlmc=&yjxkdm=0101&xxfs=&zymc=
        //  $url = "https://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=$ssdm&dwmc=$dwmc&mldm=$mldm&mlmc=&yjxkdm=$yjxkdm&xxfs=$xxfs&zymc=&pageno=$pageno";
        $jkmn = $param['mldm']; //门类
        $ssdm = $param['ssdm']; //北京
        $dxmc =$param['dwmc'];
        $where=[];
       // $where[] = ['yjxkdm_id','between',[$startid,$endid]];

        $sontype = Db::table('jx_kytype')->where('mldm_id', $jkmn)->where($where)->field('yjxkdm_id')->order('yjxkdm_id asc')->select()->toArray();
      
        /********************************** 第二种方式  *********************** */
        //  $jkmn ='';
        //  $startid = '1052'; //开始id
     //  $endid = '1452';  //结束id
    //   $where[] = ['yjxkdm_id','between',[$startid,$endid]];
     //  $where=[];
      //  $sontype = Db::table('jx_kytype')->where('zyxw_type', '2')->where($where)->field('yjxkdm_id')->order('yjxkdm_id asc')->select()->toArray();
    //  print_r($sontype);
  //   die();
        foreach ($sontype as $k => $v) {
            $yjxkdm_id = $v['yjxkdm_id'];
             $url = "https://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=$ssdm&dwmc=$dxmc&mldm=$jkmn&mlmc=&yjxkdm=$yjxkdm_id&xxfs=&zymc=";
            $url2 = parse_url($url);
            $queryData = explode('&', $url2['query']);
            $ssdm_data = explode('=', $queryData['0']); //省份
            $mldm_data = explode('=', $queryData['2']); //门类
            $yjxkdm_data = explode('=', $queryData['4']); //学科类别
       
            // DOM解析规则
            $rules = [
                // 文章标题
                'title' => ['tbody tr', 'html'],
                // 发布日期
                // 'date' => ['.meta>span:eq(0)', 'text'],
                // 文章内容
                //  'content' => ['#artibody', 'html']
            ];
           // echo $url;
            $data = QueryList::Query($url, $rules)->data;
        //  print_r($data);
        //   die();
            if (!empty($data)) {
                foreach ($data as $k => $v) {
                    $tdstr =  $v['title'];
                    $tddata = QueryList::Query($tdstr, array(
                        'txt' => array('td', 'text',),
                        'link' => array('a', 'href',)
                    ))->data;
               
                    if (!empty($tddata)) {
                        $nrurl = 'https://yz.chsi.com.cn/' . $tddata['0']['link'];
                        $nrdata = QueryList::Query($nrurl, array(
                            'txt' => array('.zsml-result tbody td', 'text', '-span'),
                            // 'link' => array('a','href',) 
                        ))->data;
                        $dxdata = QueryList::Query($nrurl, array(
                            'dxmc' => array('.zsml-summary:eq(0)', 'text'),
                            // 'link' => array('a','href',) 
                        ))->data;
          
                        //print_r($nrdata);
                        $jsonnrdata = json_encode($nrdata, JSON_UNESCAPED_UNICODE);
                    }
                    $map['ssdm'] = $ssdm_data['1'];
                    $map['mldm'] = $mldm_data['1'];
                    $map['yjxkdm'] = $yjxkdm_data['1'];
                    $map['zyly'] = $tddata['2']['txt']; //专业

                    $map['dxmc'] = $dxdata['0']['dxmc']; //大学名称
                    $map['ysx'] = $tddata['1']['txt'];

                    $map['yjfx'] = $tddata['3']['txt']; //研究方向
                    $map['xxfs'] = $tddata['4']['txt']; //学习方式

                    $map['ksfw'] = $jsonnrdata; //考试范围
                    $res = Db::table('jx_yjsxx')->insert($map);
                    // die();
                }
            }
        }
        echo 'success';
        die();
    
        return $this->buildSuccess([]);
    }   
    //单页url采集
    public function test3(){
 
        $param = $this->request->param();
        $url = $param['url'];
        $url2 = parse_url($url);
       $queryData = explode('&', $url2['query']);
       $ssdm_data = explode('=', $queryData['0']); //省份
       $mldm_data = explode('=', $queryData['2']); //门类
       $yjxkdm_data = explode('=', $queryData['4']); //学科类别
  
       // DOM解析规则
       $rules = [
           // 文章标题
           'title' => ['tbody tr', 'html'],
           // 发布日期
           // 'date' => ['.meta>span:eq(0)', 'text'],
           // 文章内容
           //  'content' => ['#artibody', 'html']
       ];
     //  echo $url;
       $data = QueryList::Query($url, $rules)->data;

       if (!empty($data)) {
           foreach ($data as $k => $v) {
               $tdstr =  $v['title'];
               $tddata = QueryList::Query($tdstr, array(
                   'txt' => array('td', 'text',),
                   'link' => array('a', 'href',)
               ))->data;
          
               if (!empty($tddata)) {
                   $nrurl = 'https://yz.chsi.com.cn/' . $tddata['0']['link'];
                   $nrdata = QueryList::Query($nrurl, array(
                       'txt' => array('.zsml-result tbody td', 'text', '-span'),
                       // 'link' => array('a','href',) 
                   ))->data;
                   $dxdata = QueryList::Query($nrurl, array(
                       'dxmc' => array('.zsml-summary:eq(0)', 'text'),
                       // 'link' => array('a','href',) 
                   ))->data;
                   //   print_r($dxdata);
                   //print_r($nrdata);
                   $jsonnrdata = json_encode($nrdata, JSON_UNESCAPED_UNICODE);
               }
               $map['ssdm'] = $ssdm_data['1'];
               $map['mldm'] = $mldm_data['1'];
               $map['yjxkdm'] = $yjxkdm_data['1'];
               $map['zyly'] = $tddata['2']['txt']; //专业

               $map['dxmc'] = $dxdata['0']['dxmc']; //大学名称
               $map['ysx'] = $tddata['1']['txt'];

               $map['yjfx'] = $tddata['3']['txt']; //研究方向
               $map['xxfs'] = $tddata['4']['txt']; //学习方式

               $map['ksfw'] = $jsonnrdata; //考试范围
               $res = Db::table('jx_yjsxx')->insert($map);
               echo 'success';
               // die();
           }
       }
    }

    //详情页采集
    public function test4(){
        $param = $this->request->param();
        $nrurl = $param['nurl'];
        //考试范围
        $nrdata = QueryList::Query($nrurl, array(
            'dxmc' => array('.zsml-summary', 'text'),
            'txt' => array('.zsml-result tbody td', 'text', '-span'),
            // 'link' => array('a','href',) 
        ))->data;
        //大学名称等
        $datadx = QueryList::Query($nrurl, array(
            'dxmc' => array('.zsml-summary', 'text'),
        ))->data;
        $jsonnrdata = json_encode($nrdata, JSON_UNESCAPED_UNICODE);
        print_r($datadx);
     $map['dxmc'] = $datadx['0']['dxmc']; //大学名称
     $map['ysx'] = $datadx['2']['dxmc'];//考试方式
     $map['ksfs'] = $datadx['1']['dxmc'];//考试方式
     $map['zyly'] = $datadx['3']['dxmc']; //专业
     $map['xxfs'] = $datadx['4']['dxmc']; //学习方式
     $map['yjfx'] = $datadx['5']['dxmc']; //研究方向
     $map['ksfw'] = $jsonnrdata; //考试范围
     $res = Db::table('jx_yjsxx')->insert($map);
    echo 'success';

    }



    //学术学位采集
    public function test7(): Response
    {
        $param = $this->request->param();
        $ssdm = $param['ssdm']; //北京
        $dxmc = $param['dwmc'];

        /********************************** 第二种方式  *********************** */
          $jkmn ='';
          $startid = $param['startid']; //开始id
         $endid = $param['endid'];  //结束id
         $where[] = ['yjxkdm_id','between',[$startid,$endid]];

        $sontype = Db::table('jx_kytype')->where('zyxw_type', '2')->where($where)->field('yjxkdm_id')->order('yjxkdm_id asc')->select()->toArray();

        foreach ($sontype as $k => $v) {
            $yjxkdm_id = $v['yjxkdm_id'];
             $url = "https://yz.chsi.com.cn/zsml/querySchAction.do?ssdm=$ssdm&dwmc=$dxmc&mldm=$jkmn&mlmc=&yjxkdm=$yjxkdm_id&xxfs=&zymc=";
            $url2 = parse_url($url);
            $queryData = explode('&', $url2['query']);
            $ssdm_data = explode('=', $queryData['0']); //省份
            $mldm_data = explode('=', $queryData['2']); //门类
            $yjxkdm_data = explode('=', $queryData['4']); //学科类别
       
            // DOM解析规则
            $rules = [
                // 文章标题
                'title' => ['tbody tr', 'html'],
                // 发布日期
                // 'date' => ['.meta>span:eq(0)', 'text'],
                // 文章内容
                //  'content' => ['#artibody', 'html']
            ];
           // echo $url;
            $data = QueryList::Query($url, $rules)->data;
        //  print_r($data);
        //   die();
            if (!empty($data)) {
                foreach ($data as $k => $v) {
                    $tdstr =  $v['title'];
                    $tddata = QueryList::Query($tdstr, array(
                        'txt' => array('td', 'text',),
                        'link' => array('a', 'href',)
                    ))->data;
               
                    if (!empty($tddata)) {
                        $nrurl = 'https://yz.chsi.com.cn/' . $tddata['0']['link'];
                        $nrdata = QueryList::Query($nrurl, array(
                            'txt' => array('.zsml-result tbody td', 'text', '-span'),
                            // 'link' => array('a','href',) 
                        ))->data;
                        $dxdata = QueryList::Query($nrurl, array(
                            'dxmc' => array('.zsml-summary:eq(0)', 'text'),
                            // 'link' => array('a','href',) 
                        ))->data;
          
                        //print_r($nrdata);
                        $jsonnrdata = json_encode($nrdata, JSON_UNESCAPED_UNICODE);
                    }
                    $map['ssdm'] = $ssdm_data['1'];
                    $map['mldm'] = $mldm_data['1'];
                    $map['yjxkdm'] = $yjxkdm_data['1'];
                    $map['zyly'] = $tddata['2']['txt']; //专业

                    $map['dxmc'] = $dxdata['0']['dxmc']; //大学名称
                    $map['ysx'] = $tddata['1']['txt'];

                    $map['yjfx'] = $tddata['3']['txt']; //研究方向
                    $map['xxfs'] = $tddata['4']['txt']; //学习方式

                    $map['ksfw'] = $jsonnrdata; //考试范围
                    $res = Db::table('jx_yjsxx')->insert($map);
                    // die();
                }
            }
        }
        return $this->buildSuccess();

    }  

    //列表页
    public function testList(): Response{
        $param = $this->request->param();
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 100;
        if (isset($param['ssdm']) && $param['ssdm'] != '') {
            $where[] =['ssdm',$param['ssdm']];
        }
        $count = Db::table('jx_school')->where($where)->count();
        $list = Db::table('jx_school')->where($where)->page((int)$page, (int)$limit)->select()->toArray();
      
        $data['count'] = $count;
        $data['list'] = $list;
        return $this->buildSuccess($data);

    }



    //科目抓取分类
    public function test5(){
        $str = '[{"txt":"(101)思想政治理论"},{"txt":"(202)俄语"},{"txt":"(621)艺术理论基础"},{"txt":"(519)造型基础(专业学位)-第3天上午8:30-11:30考"},{"txt":"\r\n                                                    \r\n                                                    \r\n                                                "},{"txt":"(101)思想政治理论"},{"txt":"(203)日语"},{"txt":"(621)艺术理论基础"},{"txt":"(519)造型基础(专业学位)-第3天上午8:30-11:30考"},{"txt":"\r\n                                                    \r\n                                                    \r\n                                                "},{"txt":"(101)思想政治理论"},{"txt":"(204)英语（二）"},{"txt":"(621)艺术理论基础"},{"txt":"(519)造型基础(专业学位)-第3天上午8:30-11:30考"},{"txt":"\r\n                                                    \r\n                                                    \r\n                                                "},{"txt":"(101)思想政治理论"},{"txt":"(241)德语"},{"txt":"(621)艺术理论基础"},{"txt":"(519)造型基础(专业学位)-第3天上午8:30-11:30考"},{"txt":"\r\n                                                    \r\n                                                    \r\n                                                "},{"txt":"(101)思想政治理论"},{"txt":"(242)法语"},{"txt":"(621)艺术理论基础"},{"txt":"(519)造型基础(专业学位)-第3天上午8:30-11:30考"}]';
       //  $str = str_replace("\r\n",'',$str);
       //  $str = str_replace('/^\s+|\s+$/m','',$str);
         $str = $this->trimall($str);
        $strarr = json_decode($str,true);
        if(!empty($strarr)){
            foreach($strarr as $k=>$v){
                  $str2 = $this->trimall($v['txt']);
                if($str2 == ''){
                  unset($strarr[$k]);
                }
            }
        }
        $endarr = array_chunk($strarr,4);
        print_r($endarr);
    }

    function trimall($str)//删除空格

    {
    
    $qian=array(" ","　","\t","\n","\r","\r\n","\r\n\r\n\r\n");
    
    $hou=array("","","","","","","");
    
    return str_replace($qian,$hou,$str);
    
    }


}
